<?php

namespace app\admin\controller\general;

use app\admin\model\Admin;
use app\admin\model\AdminLog;
use app\common\controller\BaseAdminController;

class Profile extends BaseAdminController
{
    public function index()
    {
        if($this->request->isAjax()){
            $id = $this->auth->id;
            $info = Admin::where('id',$id)->find();
            return $this->success('获取成功',null,$info);
        }
        return $this->view->fetch();
    }

    /**
     * 保存
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save()
    {
        $data = $this->request->param('row/a');
        $info = Admin::where('id',$this->auth->id)->find();
        if(!empty($data['password'])){
            $data['password'] = create_password($data['password'],$info['salt']);
        }else{
            if(isset($data['password'])){
                unset($data['password']);
            }
        }
        $info->save($data);
        return $this->success('修改成功');
    }

    /**
     * 获取日志
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function get_log()
    {
        $limit = $this->request->param('limit/d',10);
        $where = [
            ['admin_id','=',$this->auth->id]
        ];
        $list = AdminLog::where($where)->order('id','desc')
            ->paginate($limit);
        return $this->success('获取成功',null,[
            'rows'=>$list->items(),
            'total'=>$list->total()
        ]);
    }

}